package com.tsinghua.server.train.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.http.server.HttpServerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsinghua.server.common.Result;
import com.tsinghua.server.common.utils.BaseUtil;
import com.tsinghua.server.common.utils.ExcleUtil;
import com.tsinghua.server.original.entity.FxjcCyz;
import com.tsinghua.server.original.entity.FxjcJrz;
import com.tsinghua.server.original.service.FxjcCtzService;
import com.tsinghua.server.train.entity.*;
import com.tsinghua.server.train.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;

/**
 * @description: 模型训练
 * @author: Mr.Eric
 * @create: 2022/3/5 0:24
 */
@RestController
@RequestMapping(value = "/basic/data/")
@Slf4j
public class BaseDataController {


    @Resource
    private FxjcCyzBaseService fxjcCyzBaseService;
    @Resource
    private FxjcBxBaseService fxjcBxBaseService;
    @Resource
    private FxjcCtzBaseService fxjcCtzService;
    @Resource
    private FxjcYhBaseService fxjcYhBaseService;
    @Resource
    private FxjcZqBaseService fxjcZqBaseService;


    @GetMapping(value = "cyz/page")
    public Result baseCyz(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt
    ) {
        QueryWrapper<FxjcCyzBase> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcCyzBase> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcCyzBaseService.page(page,queryWrapper));
    }

    @GetMapping(value = "bx/page")
    public Result baseBX(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt
    ) {
        QueryWrapper<FxjcBxBase> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcBxBase> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcBxBaseService.page(page,queryWrapper));
    }


    @GetMapping(value = "ctz/page")
    public Result baseCTZ(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt
    ) {
        QueryWrapper<FxjcCtzBase> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcCtzBase> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcCtzService.page(page,queryWrapper));
    }


    @GetMapping(value = "yh/page")
    public Result baseYH(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt
    ) {
        QueryWrapper<FxjcYhBase> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcYhBase> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcYhBaseService.page(page,queryWrapper));
    }

    @GetMapping(value = "zq/page")
    public Result baseZQ(
            @RequestParam(name = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(name = "zwzt", required = false) String zwzt
    ) {
        QueryWrapper<FxjcZqBase> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(zwzt)) {
            queryWrapper.like("zwzt",zwzt);
        }
        Page<FxjcZqBase> page = new Page<>(pageNo , pageSize);
        return Result.succ(fxjcZqBaseService.page(page,queryWrapper));
    }



    /**
     * 执行python脚本
     * @param pyVo
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @PostMapping("exec/python")
    public Result execPyScript(@RequestBody PyVo pyVo) throws IOException, InterruptedException {
        HashMap<String,String> pyParam = BaseUtil.getPyParam(pyVo.getPyType(),pyVo.getDataType());
        String[] args;
        if(pyVo.getPyType() == 1) {
            args = new String[]{"python3",pyParam.get("pyPath"),pyParam.get("InputTable"), pyParam.get("OutputTable"), pyVo.getDataType()};
        } else if(pyVo.getPyType() == 2) {
            args = new String[]{"python3",pyParam.get("pyPath"),pyParam.get("InputTable"), pyParam.get("OutputTable"),"模型名称", pyVo.getDataType()};
        } else {
            args = new String[]{"python3",pyParam.get("pyPath"),pyParam.get("InputTable"), pyParam.get("OutputTable"),"模型名称", pyVo.getDataType()};
        }
        Process proc = Runtime.getRuntime().exec(args);
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
        proc.waitFor();
        return Result.succ(true);
    }

    /**
     * 导入excle
     * @param file
     * @param dataType
     * @return
     */
    @PostMapping(value = "import")
    public Result importExcle(@RequestParam("file") MultipartFile file,@RequestParam("dataType") Integer dataType){
        switch (dataType){
            case 1:
                long start = System.currentTimeMillis();
                List<FxjcCtzBase> fxjcCtzBases = ExcleUtil.importData(file,1,FxjcCtzBase.class);
                fxjcCtzService.saveBatch(fxjcCtzBases);
                long end = System.currentTimeMillis();
                log.info("耗时{}",end-start);
                break;
            case 2:
                List<FxjcCyzBase> fxjcCyzBases = ExcleUtil.importData(file,1,FxjcCyzBase.class);
                fxjcCyzBaseService.saveBatch(fxjcCyzBases);
                break;
            case 3:
                List<FxjcYhBase> fxjcYhBases = ExcleUtil.importData(file,1,FxjcYhBase.class);
                fxjcYhBaseService.saveBatch(fxjcYhBases);
                break;
            case 4:
                List<FxjcBxBase> fxjcBxBases = ExcleUtil.importData(file,1,FxjcBxBase.class);
                fxjcBxBaseService.saveBatch(fxjcBxBases);
                break;
            case 5:
                List<FxjcZqBase> fxjcZqBases = ExcleUtil.importData(file,1,FxjcZqBase.class);
                fxjcZqBaseService.saveBatch(fxjcZqBases);
                break;
        }
        return Result.succ(null);
    }

    @PostMapping(value = "reimport")
    @Transactional
    public Result reImportExcle(@RequestParam("file") MultipartFile file,@RequestParam("dataType") Integer dataType){
        switch (dataType){
            case 1:
                long start = System.currentTimeMillis();
                List<FxjcCtzBase> fxjcCtzBases = ExcleUtil.importData(file,1,FxjcCtzBase.class);
                fxjcCtzService.delAll();
                fxjcCtzService.saveBatch(fxjcCtzBases);
                long end = System.currentTimeMillis();
                log.info("耗时{}",end-start);
                break;
            case 2:
                List<FxjcCyzBase> fxjcCyzBases = ExcleUtil.importData(file,1,FxjcCyzBase.class);
                fxjcCyzBaseService.delAll();
                fxjcCyzBaseService.saveBatch(fxjcCyzBases);
                break;
            case 3:
                List<FxjcYhBase> fxjcYhBases = ExcleUtil.importData(file,1,FxjcYhBase.class);
                fxjcYhBaseService.delAll();
                fxjcYhBaseService.saveBatch(fxjcYhBases);
                break;
            case 4:
                List<FxjcBxBase> fxjcBxBases = ExcleUtil.importData(file,1,FxjcBxBase.class);
                fxjcBxBaseService.delAll();
                fxjcBxBaseService.saveBatch(fxjcBxBases);
                break;
            case 5:
                List<FxjcZqBase> fxjcZqBases = ExcleUtil.importData(file,1,FxjcZqBase.class);
                fxjcZqBaseService.delAll();
                fxjcZqBaseService.saveBatch(fxjcZqBases);
                break;
        }
        return Result.succ(null);
    }


    /**
     * 数据导出
     * @param dataType
     * @param response
     */
    @GetMapping("export")
    public void export(@RequestParam("dataType") Integer dataType,HttpServletResponse response){

        switch (dataType){
            case 1:
                List<FxjcCtzBase> fxjcCtzBases = fxjcCtzService.list();
                ExcleUtil.exportExcel(fxjcCtzBases,null,"fxjcCtzBases",FxjcCtzBase.class,"fxjcCtzBases.xlsx", response);
                break;
            case 2:
                List<FxjcCyzBase> fxjcCyzBases = fxjcCyzBaseService.list();
                ExcleUtil.exportExcel(fxjcCyzBases,null,"fxjcCyzBases",FxjcCyzBase.class,"fxjcCyzBases.xlsx", response);
                break;
            case 3:
                List<FxjcYhBase> fxjcYhBases = fxjcYhBaseService.list();
                ExcleUtil.exportExcel(fxjcYhBases,null,"fxjcYhBases",FxjcYhBase.class,"fxjcYhBases.xlsx", response);
                break;
            case 4:
                List<FxjcBxBase> fxjcBxBases = fxjcBxBaseService.list();
                ExcleUtil.exportExcel(fxjcBxBases,null,"FxjcBxBase",FxjcBxBase.class,"FxjcBxBase.xlsx", response);
                break;
            case 5:
                List<FxjcZqBase> fxjcZqBases = fxjcZqBaseService.list();
                ExcleUtil.exportExcel(fxjcZqBases,null,"fxjcZqBases",FxjcZqBase.class,"fxjcZqBases.xlsx", response);
                break;
        }
    }



}
